﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Entities;

namespace Stationery
{
    public partial class Stationery : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ASPxRoundPanelAdvertise.Visible = false;

                Set_Menu_Header();
                Set_Menu_Left();
                Set_GoodProduct();
                Set_Advertise();
                Set_EmailSupport();
            }
            Set_InfoCart();
            LabelOnline.Text = Application["visitors_online"].ToString();
            LabelHomnay.Text = Application["HomNay"].ToString();
            LabelTotalOnline.Text = Application["TatCa"].ToString();
        }

        
        protected void ASPxNavBarLeft_ItemClick(object source, DevExpress.Web.ASPxNavBar.NavBarItemEventArgs e)
        {
            int i = 0;
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(UtilsURL.order);
        }


        protected string MasterGetLinkDetail(object pID)
        {
            BLL_Product bll_Product = new BLL_Product();
            string strID_PARENT = "";
            int intID;
            string strURL = "";
            intID = Convert.ToInt32(pID);
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(intID))[0];
                strID_PARENT = product.ID_MENUURL.ToString();
            }
            catch
            {
                return "";
            }
            strURL = String.Format("WebProduct.aspx?id={0}&id_child={1}", strID_PARENT, intID);

            return strURL;
        }
        

        private void Set_Menu_Header()
        {
            //tao menu cho phan header
            BLL_MenuURL bll_MenuURL = new BLL_MenuURL();
            List<MenuURL> lstMenuURL = bll_MenuURL.Lst_MenuURL(bll_MenuURL.Get_MenuURL_Type(MenuURL.row));
            foreach (MenuURL menuURL in lstMenuURL)
            {
                ASPxMenuHeader.Items.Add(menuURL.NAME, menuURL.SEQUENCE.ToString(), menuURL.IMAGE_DIRECTORY, menuURL.URL);
            }
        }
        private void Set_Menu_Left()
        {
            //tao menu cho phan ben trai
            BLL_MenuURL bll_MenuURL = new BLL_MenuURL();
            List<MenuURL> lstMenuURL = bll_MenuURL.Lst_MenuURL(bll_MenuURL.Get_MenuURL_Type(MenuURL.column));
            int intCount = 0;
            foreach (MenuURL menuURL in lstMenuURL)
            {
                ASPxNavBarLeft.Groups.Add(menuURL.NAME, "parent_" + intCount, "", menuURL.URL, "_self");
                //ứng với mối group, load con lên
                List<MenuURL> lstMenuURL_Dynamic = bll_MenuURL.Lst_MenuURL(bll_MenuURL.Get_MenuURL_Dynamic(String.Format("ID_PARENT={0}", menuURL.ID)));
                foreach (MenuURL menuURL_Dynamic in lstMenuURL_Dynamic)
                {
                    ASPxNavBarLeft.Groups[intCount].Items.Add(menuURL_Dynamic.NAME, "child_" + ASPxNavBarLeft.Groups[intCount].Items.Count.ToString(), "", String.Format("WebProduct.aspx?id={0}", menuURL_Dynamic.ID), "_self");
                }
                intCount = intCount + 1;
            }
        }
        private void Set_GoodProduct()
        {
            //tao danh sach san pham ban chay
            BLL_Product bll_Product = new BLL_Product();
            ASPxDataViewGoodProduct.DataSource = bll_Product.Get_GoodProduct();
            ASPxDataViewGoodProduct.DataBind();
        }
        private void Set_InfoCart()
        {
            // Ghi thong tin gia tri gio hang len label
            if (Session[Utils.SESSION_CART] != null)
            {
                List<Order> lstCart = (List<Order>)Session[Utils.SESSION_CART];
                LabelCart.Text = String.Format(Utils.label_cart, lstCart.Count.ToString("### ###"));
                LabelTotal.Text = String.Format(Utils.label_cart_total, lstCart.Sum(o => o.TOTAL).ToString("### ### ###"));
            }
            else
            {
                LabelCart.Text = String.Format(Utils.label_cart, 0);
                LabelTotal.Text = String.Format(Utils.label_cart_total, 0);
            }
        }
        private void Set_Advertise()
        {
            BLL_Advertise bll_Advertise = new BLL_Advertise();
            ASPxDataViewAdvertise1.DataSource = bll_Advertise.Get_Advertise_All();
            ASPxDataViewAdvertise1.DataBind();
        }
        private void Set_EmailSupport()
        {
            BLL_User bll_User = new BLL_User();
            DataListSupport.DataSource = bll_User.Get_Nick_Support();
            DataListSupport.DataBind();
        }
    }
}
